#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project : proposal-api 
# @File    : requests_base.py
# @IDE     : PyCharm 
# @Author  :ZH
# @Time    : 2024/10/28 10:48
import requests
from sentry_sdk.integrations import httpx

__all__ = ['server_await_response_util', 'server_response']


class ServerAwaitRequestUtil:
    __doc__ = '异步服务请求方法'

    async def get(self, url, params=None, headers=None, *args, **kwargs):
        if headers is None:
            headers = {}
        if params is None:
            params = {}
        if 'json_data' in kwargs:
            del kwargs['json_data']

        async with httpx.AsyncClient(verify=False) as client:
            response = await client.get(url, params=params, headers=headers, *args, **kwargs)
            response.raise_for_status()
        component = response.json()
        return component

    async def post(self, url, params=None, headers=None, json_data=None, *args, **kwargs):
        if headers is None:
            headers = {}
        if params is None:
            params = {}
        if json_data is None:
            json_data = {}

        async with httpx.AsyncClient(verify=False) as client:
            response = await client.post(url, params=params, headers=headers, json=json_data, *args, **kwargs)
            response.raise_for_status()
        component = response.json()
        return component

    async def put(self, url, params=None, headers=None, json_data=None, *args, **kwargs):
        async with httpx.AsyncClient(verify=False) as client:
            response = await client.put(url, params=params, headers=headers, json=json_data, *args, **kwargs)
            response.raise_for_status()
        component = response.json()
        return component

    async def delete(self, url, params=None, headers=None, *args, **kwargs):
        async with httpx.AsyncClient(verify=False) as client:
            response = await client.delete(url, params=params, headers=headers, *args, **kwargs)
            response.raise_for_status()
        component = response.json()
        return component

    async def patch(self, url, params=None, headers=None, json_data=None, *args, **kwargs):
        async with httpx.AsyncClient(verify=False) as client:
            response = await client.patch(url, params=params, headers=headers, json=json_data, *args, **kwargs)
            response.raise_for_status()
        component = response.json()
        return component


class ServerRequestUtil:
    """服务请求方法"""

    def get(self, url, params=None, headers=None, **kwargs):
        if headers is None:
            headers = {}
        if params is None:
            params = {}

        response = requests.get(url, params=params, headers=headers,verify=False, **kwargs)
        response.raise_for_status()
        return response.json()

    def post(self, url, data=None, json=None, headers=None, json_data=None, **kwargs):
        if headers is None:
            headers = {}
        if json_data and json is None:
            json = json_data
        if data is None and json is None:
            json = {}

        response = requests.post(url, data=data, json=json, headers=headers,verify=False, **kwargs)
        response.raise_for_status()
        return response.json()

    def put(self, url, data=None, json=None, headers=None, **kwargs):
        response = requests.put(url, data=data, json=json, headers=headers,verify=False, **kwargs)
        response.raise_for_status()
        return response.json()

    def delete(self, url, params=None, headers=None, **kwargs):
        response = requests.delete(url, params=params, headers=headers,verify=False, **kwargs)
        response.raise_for_status()
        return response.json()

    def patch(self, url, data=None, json=None, headers=None, **kwargs):
        response = requests.patch(url, data=data, json=json, headers=headers,verify=False, **kwargs)
        response.raise_for_status()
        return response.json()


server_await_response_util = ServerAwaitRequestUtil()
server_response = ServerRequestUtil()